// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器
		if(this.getHttpInfo().body){
			let body = JSON.parse(this.getHttpInfo().body)
			this.parames=body
		}
		

	},
	async getMoldById(obj) {
		console.log(obj,"======")
		if (!obj.product_id) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: '产品不可为空'
			}
		}
		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		const res = await db.collection("threed-list").where("product_id==" + obj.product_id).limit(1).get();
		return {
			data: res.data
		}
	},
	async getDesignByUser() {
		const parames=this.parames
		//获取用户的设计
		if (!parames.user_id || !parames.product_id) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: 'user_id或product_id不可为空'
			}
		}

		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		console.log(parames,"==========")
		const design = await db.collection("custom-design").where("product_id==" + parames.product_id + " && " +
			"user_id==" + parames.user_id).getTemp()
		 const res=await db.collection(design,"threed-list").field('mold_id{name,gltf_url,qk},design_data').get()
		return {
			data: res.data
		}

	},
	
	async addDesign(obj) {

		if (!obj.mold_id) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: '模型不可为空'
			}
		}
		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		const mold_id = obj.mold_id;
		const url = obj.url;
		const str = obj.diy_color_show;
		let diyColorShow;
		if (str) {
			diyColorShow = str.split(',');
		}
		// console.log("=========",str,diyColorShow)
		const data = {
			mold_id,
			product_id: parseInt(obj.product_id),
			user_id: parseInt(obj.user_id),
			design_data: {
				url,
				diyColorShow
			}
		}
		// console.log("++++",data)
		const res = await db.collection("custom-design").add(data);
		// console.log('++++++++',res.idid)
		return {
			data: {
				id: res.id
			}
		}
	}


}